/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game.platformer.levels;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import game.platformer.platformer;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
/**
 *
 * @author cow
 */
public class levelPlatform{
	//varBlock-------------------------
	private Sprite platformRep;
	private Sprite decoration;
	private Body phyRep;
	private PhysicsConnector imageToBodyLink;
	private boolean placed;
	//---------------------------------
	//constants------------------------
	public static final FixtureDef platformDef = PhysicsFactory.createFixtureDef(1f, 0f, .5f);
	//---------------------------------


	/**
	 * creates a platform out of ht etexture region
	 * @param imageTr  The texture region to use for the sprite
	 */
	public levelPlatform(TextureRegion imageTr, int xPos, int yPos){
		decoration = null;
		platformRep = new Sprite(xPos, yPos, imageTr);
		phyRep = PhysicsFactory.createBoxBody(platformer.getPhysicsWorld(), platformRep,
			BodyType.StaticBody, platformDef);
		imageToBodyLink = new PhysicsConnector(platformRep, phyRep, false, false);

		placed = false;
	}

	/**
	 * creates a platform out of the texture region with decoration
	 * @param imageTr  The texture region to use for the sprite
	 */
	public levelPlatform(TextureRegion imageTr, TextureRegion decor, int xPos, int yPos){
		platformRep = new Sprite(xPos, yPos, imageTr);
		decoration = new Sprite(xPos, yPos - decor.getHeight(), decor);
		phyRep = PhysicsFactory.createBoxBody(platformer.getPhysicsWorld(), platformRep,
			BodyType.StaticBody, platformDef);
		imageToBodyLink = new PhysicsConnector(platformRep, phyRep, false, false);

		placed = false;
	}

	/**
	 * places the platform into the world
	 * @param x
	 * @param y
	 */
	public void place(){
		if(placed) return; //early exit condition
		if(decoration != null)
			platformer.getScreenManager().getMapScreen().attachChild(decoration);
		if(platformRep != null)
			platformer.getScreenManager().getMapScreen().attachChild(platformRep);
		if(phyRep != null)
			platformer.getPhysicsWorld().registerPhysicsConnector(imageToBodyLink);
		placed = true;
	}

	public Sprite getSpriteRep(){
		return platformRep;
	}

	public Body getBodyRep(){
		return phyRep;
	}

	public PhysicsConnector getConnector(){
		return imageToBodyLink;
	}

	public Sprite getDecor(){
		return decoration;
	}
}
